/**
 * Created by sangcixiang on 2017/5/7.
 */
import {Injectable}       from '@angular/core';
import { ToastController, LoadingController, Platform, Loading} from 'ionic-angular';
import { Toast }          from '@ionic-native/toast';
import { SpinnerDialog }  from '@ionic-native/spinner-dialog';
import { NativeStorage }  from '@ionic-native/native-storage';
@Injectable()
export class NativeService{
  private loading:Loading;
  private loadingIsOpen: boolean = false;
  constructor(private platform: Platform,
              private toastCtrl: ToastController,
              private toast:Toast,
              private storage:NativeStorage,
              private spinnerDialog: SpinnerDialog,
              private loadingCtrl: LoadingController){

  }

  storageSetValue(key:string,value:string){
    if (this.isMobile()){
      this.storage.setItem(key,value);
    }else {
      localStorage.setItem(key,value);
    }
  }
  storageGetValue(key,callback){
    if (this.isMobile()){
      this.storage.getItem(key).then(value=>{
        callback(value);
      });
    }else {
      callback(localStorage.getItem(key));
    }
  }

  /**
   * 是否真机环境
   * @return {boolean}
   */
  isMobile(): boolean {
    return this.platform.is('mobile') && !this.platform.is('mobileweb');
  }

  /**
   * 是否android真机环境
   * @return {boolean}
   */
  isAndroid(): boolean {
    return this.isMobile() && this.platform.is('android');
  }

  /**
   * 是否ios真机环境
   * @return {boolean}
   */
  isIos(): boolean {
    return this.isMobile() && (this.platform.is('ios') || this.platform.is('ipad') || this.platform.is('iphone'));
  }

  /**
   * 统一调用此方法显示提示信息
   * @param message 信息内容
   * @param duration 显示时长
   */
  showToast(message: string = '操作完成', duration: number = 2000): void {
    if(!this.isMobile()){
      this.toastCtrl.create({
        message: message,
        duration: duration,
        position: 'middle',
        showCloseButton: false
      }).present();

    }else {
      this.toast.show(message, "2000", 'center').subscribe( toast => {console.log(toast)});
    }
  };
  /**
   * 统一调用此方法显示loading
   * @param content 显示的内容
   */
  showLoading(content: string = '正在加载中...'): void {
    if(this.isMobile()){
      this.spinnerDialog.show(null,content);
    }else {
      if (!this.loadingIsOpen) {
        this.loadingIsOpen = true;
        this.loading = this.loadingCtrl.create({
          content: content
        });
        this.loading.present();
      }
    }
  };

  /**
   * 关闭loading
   */
  hideLoading(): void {
    if (this.isMobile()){
      this.spinnerDialog.hide();
    }else {
      this.loadingIsOpen && this.loading.dismiss();
      this.loadingIsOpen = false;
    }

  };
  dateFormat(date:Date,format):string{
    var o = {
      "M+" : date.getMonth()+1,                 //月份
      "d+" : date.getDate(),                    //日
      "h+" : date.getHours(),                   //小时
      "m+" : date.getMinutes(),                 //分
      "s+" : date.getSeconds(),                 //秒
      "q+" : Math.floor((date.getMonth()+3)/3), //季度
      "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(format))
      format=format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
      if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return format;
  }
  /**
   * 获取网络类型 如`unknown`, `ethernet`, `wifi`, `2g`, `3g`, `4g`, `cellular`, `none`
   */
  getNetworkType(): string {
    if (!this.isMobile()) {
      return 'wifi';
    }
    return 'wifi';
  }

  /**
   * 判断是否有网络
   * @returns {boolean}
   */
  isConnecting(): boolean {
    return this.getNetworkType() != 'none';
  }


  delHtmlTag(str):string {
    return str.replace(/<[^>]+>|&nbsp;/g,"");//去掉所有的html标记
  }

}
